﻿using GalaSoft.MvvmLight.Messaging;
using HRM.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRM.Model
{
    public class PieceSalaryBonusService : IEntityService<LoaiLuongThuong>
    {

        public List<LoaiLuongThuong> GetAll()
        {
            using (HRMEntities entities = new HRMEntities())
            {
                return entities.LoaiLuongThuongs.ToList();
            }
        }

        public bool Add(LoaiLuongThuong entity)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                entities.LoaiLuongThuongs.Add(entity);
                entities.SaveChanges();
                return true;
            }

        }

        public bool Update(LoaiLuongThuong entity)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var items = entities.LoaiLuongThuongs.Where(m => m.MaLoai == entity.MaLoai).FirstOrDefault();
                if (items != null)
                {
                    items.TenLoaiLuong = entity.TenLoaiLuong;
                    entities.SaveChanges();
                    return true;
                }
                return false;
            }
        }

        public bool Delete(LoaiLuongThuong entity)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var items = entities.LoaiLuongThuongs.Where(m => m.MaLoai == entity.MaLoai).FirstOrDefault();
                {
                    if (items != null)
                    {
                        entities.LoaiLuongThuongs.Remove(entity);
                        return true;
                    }
                    try
                    {
                        entities.SaveChanges();
                        return true;
                    }
                    catch
                    {
                         Messenger.Default.Send(StatusMessage.KeyDependentMessage());
                    }
                }
                return false;
            }
        }

        public bool DeleteList(List<LoaiLuongThuong> entity)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                foreach (var item in entity)
                {
                    var query = entities.LoaiLuongThuongs.Where(x => x.MaLoai == item.MaLoai).FirstOrDefault();
                    entities.LoaiLuongThuongs.Remove(query);
                }
                try
                {
                    entities.SaveChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    Messenger.Default.Send(StatusMessage.KeyDependentMessage());
                }
                return true;
            }
        }

        public List<LoaiLuongThuong> GetPaging(int pageIndex, int pageSize)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                return entities.LoaiLuongThuongs.OrderBy(p => p.MaLoai).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }
        }

        public int GetNumberOfPages(int pageSize)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                int count = entities.LoaiLuongThuongs.Count();
                int pages = count / pageSize;
                if (count > pages * pageSize) return pages + 1;
                return pages;
            }
        }

        public object StatusMe { get; set; }
    }
}
